package kevin.framework.basic.common.utils;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

/**
 * @Description: 用户操作记录实体
 * @author kevin.huang
 * @date 2016年8月29日 上午10:01:01
 * @version v1.0.0
 */
public class OperatorRecord {

	private static SerializeConfig dateTimeSerialize = new SerializeConfig();
	static String dateTimeFormat = "yyyy-MM-dd HH:mm:ss";
	static {
		dateTimeSerialize.put(Date.class, new SimpleDateFormatSerializer(dateTimeFormat));
		dateTimeSerialize.put(Timestamp.class, new SimpleDateFormatSerializer(dateTimeFormat));
	}
	String contextPath;

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	String controller;
	String action;

	public String getController() {
		return controller;
	}

	public void setController(String controller) {
		this.controller = controller;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	String cmd;

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 操作描述
	 **/
	String opertorDesp = null;

	public String getOpertorDesp() {
		return opertorDesp;
	}

	public void setOpertorDesp(String opertorDesp) {
		this.opertorDesp = opertorDesp;
	}

	/** 用户id **/
	String userId;
	/** 异常信息（没有异常则为空） **/
	Exception exception = null;
	/** 操作是否成功 **/
	boolean isSuccesful;
	/** 操作前的数据，删除、新增、修改等有数据变化的操作会存在该字段值，否则为空 **/
	String opertorData;

	/** 用户ip **/
	String ip;

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	/** 操作前的数据 **/
	public String getOpertorData() {
		return opertorData;
	}

	/** 操作前的数据 **/
	public void setOpertorData(Object opertorData) {
		if (opertorData != null) {
			if ("java.lang.String".equals(opertorData.getClass().getName())) {
				if (StringUtils.isEmpty(String.valueOf((opertorData)))) {
					return;
				}
			}
			JSONObject jobj = new JSONObject();
			/*
			 * if("java.util.ArrayList".equals(opertorData.getClass().getName())){ List
			 * tmpList =(List) opertorData; if(tmpList.size()>0) jobj.put("className",
			 * tmpList.get(0).getClass().getName()); }else jobj.put("className",
			 * opertorData.getClass().getName());
			 */
			jobj.put("data", opertorData);
			this.opertorData = JSONObject.toJSONString(jobj, dateTimeSerialize,
					SerializerFeature.DisableCircularReferenceDetect);
		}
	}

	/** 操作是否成功 **/
	public boolean isSuccesful() {
		return isSuccesful;
	}

	/** 操作是否成功 **/
	public void setSuccesful(boolean isSuccesful) {
		this.isSuccesful = isSuccesful;
	}

	/******
	 * 异常信息，当出现异常时候exception不为空，否则为空
	 *****/
	public Exception getException() {
		return exception;
	}

	/******
	 * 异常信息，当出现异常时候exception不为空，否则为空
	 *****/
	public void setException(Exception exception) {
		this.exception = exception;
	}

	/***
	 * 用户id
	 ***/
	public String getUserId() {
		return userId;
	}

	/***
	 * 用户id
	 ***/
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/****
	 * 操作时间
	 *****/
	public Date getOpertorTime() {
		return new Date();
	}

	public String toJsonString() {
		return JSONObject.toJSONString(this, dateTimeSerialize, SerializerFeature.DisableCircularReferenceDetect);
	}

	Map<String, String[]> requestParamMap;

	public Map<String, String[]> getRequestParamMap() {
		return requestParamMap;
	}

	public void setRequestParamMap(Map<String, String[]> requestParamMap) {
		this.requestParamMap = requestParamMap;
	}

}
